package com.cn.suplayer.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.suplayer.R;
import com.cn.suplayer.Util;
import com.cn.suplayer.backTask.BackTask;
import com.cn.suplayer.bean.VideoModel;

import java.util.List;

/**
 * Created by ZLM on 2016/6/17.
 * TODO: Write something
 */
public class DirectorDetailsAdapter extends BaseAdapter {

    public Context context;
    public List<VideoModel> videoModelList;

    public DirectorDetailsAdapter(Context context, List<VideoModel> videoList) {
        this.context = context;
        videoModelList = videoList;
    }

    @Override
    public int getCount() {
        return videoModelList.size();
    }

    @Override
    public Object getItem(int position) {
        return videoModelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_directordetails, null);
            holder=new Holder();
            holder.imgLoad=(ImageView)convertView.findViewById(R.id.loadImage);
            holder.displayName=(TextView)convertView.findViewById(R.id.videoName);
            holder.info=(TextView)convertView.findViewById(R.id.videoInfo);
            convertView.setTag(holder);
        }else{
            holder=(Holder)convertView.getTag();
        }
        VideoModel model= videoModelList.get(position);
        getBitmap(model.getPath(), holder.imgLoad);
        holder.displayName.setText(model.getDisplayName());
        holder.info.setText(model.getDateAdd()+";"+getDuration(model.getDuration())+";"+getSize(model.getSize()));

        return convertView;
    }

    public String getDuration(long d){
        long min=d/1000/60;
        long sec=d/1000%60;

        return min+"H:"+sec+"S" ;
    }

    public String getSize(long size){
        long l=size/1024/1024/1024;
        long ll=size/1024/1024%1024;
        return l+"."+ll+"GB";
    }

    class Holder {
        ImageView imgLoad;
        TextView displayName;
        TextView info;
    }


    public void getBitmap(String path,ImageView imageView){
        BackTask task=new BackTask();
        task.setBitmapTag(imageView);
        task.setWidth(Util.dp2px(context,80));
        task.execute(path);
    }
}
